// 存放秒杀交互模块
//JavaScript 模块化
var seckill = {
    // 封装秒杀相关ajax的url
    URL: {
        now: function() {
            return '/seckill/time/now';
        },
        exposer : function(seckillId){
        	return "/seckill/" + seckillId + "/exposer";
        },
        execution : function(seckillId , md5){
        	//执行秒杀地址
        	return "/seckill/" + seckillId + "/" + md5 + "/execution";
        }
    },
    //验证手机号
    validatePhone: function(phone) {
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    },
    //处理秒杀逻辑
    handleSeckill : function(seckillId , node){
    	// 获取秒杀地址，控制显示逻辑，执行秒杀
    	node.hide()
    		.html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');//按钮
    	$.post(seckill.URL.exposer(seckillId),{},function(result){
    		//在回调函数中执行回调流程
    		if(result && result['success']){
    			var exposer = result['data'];
    			if(exposer['exposed']){
    				//开启秒杀
    				var md5 = exposer['md5'];
    				//在按钮上绑定一次事件
    				$("#killBtn").one("click",function(){
    					//执行秒杀请求
    					//1.先禁用按钮
    					$(this).addClass("disabled");
    					//2.发送秒杀请求
    					$.post(seckill.URL.execution(seckillId,md5),{},function(result){
    						if(result && result['data']){
    							var killResult = result['data'];
    							var state = killResult['state'];
    							var stateInfo = killResult['stateInfo'];
    							//3.显示秒杀结果
    							node.html('<span class="label label-success">'+stateInfo+'</span>');
    						}
    					});
    				});
    				node.show();
    			}else{
    				//未开启秒杀
    				var now = exposer['now'];
    				var start = exposer['start'];
    				var end = exposer['end'];
    				//再次执行countDown
    				seckill.countdown(seckillId,now,start,end);
    			}
    		}else if(result && !result['success']){
    			node.html('<span class="label label-danger">'+stateInfo+'</span>');
    			node.show();
    		}
    	});
    },
    //秒杀倒计时
    countdown: function(seckillId, nowTime, startTime, endTime) {
    	var seckillBox  = $("#seckill-box");
    	//时间判断
    	if(nowTime > endTime){
    		//秒杀活动已经结束
    		seckillBox.html("秒杀结束!");
    	}else if(nowTime < startTime){
    		//秒杀未开始，计时操作
    		// jquery countDown插件操作
    		var killTime = new Date(startTime + 1000);
    		seckillBox.countdown(killTime , function(event){
    			//控制时间的格式
    			var format = event.strftime('秒杀倒计时 : %D天 %H时 %M分 %S秒');
    			seckillBox.html(format);
    		}).on('finish.countdown',function(){
    			//倒计时结束
    			//获取秒杀地址，控制逻辑实现，执行秒杀
    			seckill.handleSeckill(seckillId , seckillBox);
    		});
    	}else{
    		// 秒杀开始
    		// 处理秒杀逻辑
    		seckill.handleSeckill(seckillId , seckillBox);
    	}
    },
    //详情页的秒杀逻辑
    detail: {
        init: function(param) {
            //用户手机验证和登录，计时交互

            //规划交互流程

            //在cooki中查找手机号
            var seckillPhone = $.cookie('seckillPhone');
            var startTime = param['startTime'];
            var endTime = param['endTime'];
            var seckillId = param['seckillId'];

            //验证手机号
            if (!seckill.validatePhone(seckillPhone)) {
                //绑定手机号
                var killPhoneModal = $("#killPhoneModal");
                killPhoneModal.modal({
                    show: true, //显示弹出层
                    backdrop: 'static', //禁止位置关闭
                    keyboard: false //关闭键盘事件
                });
                //绑定点击秒杀按钮事件
                $("#killPhoneBtn").click(function() {
                    var inputPhone = $("#killPhoneKey").val();
                    if (seckill.validatePhone(inputPhone)) {
                        //电话写入cookie
                        $.cookie("seckillPhone", inputPhone, { expires: 7, path: "/seckill" })
                            //刷新页面
                        window.location.reload();
                    } else {
                        $("#killPhoneMessage").hide().html('<label class="label label-danger">手机号错误!</label>').show(300);
                    }
                });
            }
            //已经登录
            //计时交互
            $.get(seckill.URL.now(), {}, function(result) {
                if (result && result['success']) {
                    var nowTime = result['data'];
                    //时间判断
                    seckill.countdown(seckillId, nowTime, startTime, endTime);
                } else {

                }
            });
        }
    }
}
